/**
 * 作者：Administrator
 * 创建时间：2012-4-28 下午2:23:01
 */
package dragon.modules.system.menu.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.po.SysRoleMenu;
import dragon.core.util.StringUtils;

/**
 * 实现功能：
 */
public class RoleMenuDaoImpl {

	public List<SysRoleMenu> get(int menu) throws ExceptionHandler {
		String sql = "Select * from " + SysRoleMenu.tableName + " WHERE menu=" + menu;
		List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
		List<Map<String, Object>> l = DML.query(sql);
		for (Map<String, Object> m : l) {
			SysRoleMenu rm = new SysRoleMenu();
			rm.setMenu(Integer.parseInt(m.get("MENU").toString()));
			rm.setKind(m.get("KIND").toString());
			rm.setTid(Integer.parseInt(m.get("TID").toString()));
			list.add(rm);
		}
		return list;
	}

	public int SaveRoleMenu(SysRoleMenu rolemenu) throws ExceptionHandler {
		return DML.saveOrUpdateObject(rolemenu);
	}

	/**
	 * tids 为空 按menu=? and kind=? 删除 tids 不为空 按 menu=? and kind=? and tid not in(?) 删除
	 * 
	 * @param menu
	 * @param kind
	 * @param tids
	 * @return
	 * @throws ExceptionHandler
	 */
	public int DeleteRoleMenu(int menu, String kind, String tids) throws ExceptionHandler {
		String sql = "";
		if (StringUtils.isEmpty(tids)) {
			sql = "delete from " + SysRoleMenu.tableName + " where Menu=" + menu + " and Kind='" + kind + "'";
		} else {
			sql = "delete from " + SysRoleMenu.tableName + " where Menu=" + menu + " and Kind='" + kind + "' and tid not in (" + tids + ")";
		}
		return DML.executeUpdate(sql);
	}

	public boolean IsExist(SysRoleMenu rolemenu) throws ExceptionHandler {
		SysRoleMenu limit = DML.loadObject(SysRoleMenu.class, " WHERE MENU=? and KIND=? and TID=?", rolemenu.getMenu(), rolemenu.getKind(), rolemenu.getTid());
		return limit != null ? true : false;
	}

	public int DeleteOneRoleMenu(int menu, String kind, String tid) throws ExceptionHandler {
		String sql = "delete from " + SysRoleMenu.tableName + " where Menu=" + menu + " and Kind='" + kind + "' and Tid =" + tid;
		return DML.executeUpdate(sql);
	}

}
